<?php

namespace app\system\controller;

use think\Request;
use app\system\model\SysAdminModel;
use app\system\model\SysRoleModel;

class AdminUser extends MainFrame
{
    public function showList(Request $request, SysAdminModel $sysAdminModel)
    {
        $keyword = $request->param("keyword");
        $pageSize = 10;
        $userList = $sysAdminModel->getUserListWithPaginate($keyword, $pageSize);
        $count = $userList->total();
        $this->assign([
            "userList" => $userList,
            "count" => $count
        ]);
        // dump($sysAdminModel->getLastSql());die;
        return $this->fetch();
    }

    public function userAdd(Request $request, SysAdminModel $sysAdminModel, SysRoleModel $sysRoleModel)
    {
        if ($request->isAjax()) {
            $data = $request->param();
            $data["pwd"] = md5($data["pwd"]);
            $vaResult = $this->validate($data, [
                "username" => "unique:sys_admin",
                "email" => "unique:sys_admin",
                "phone" => "unique:sys_admin",
            ], [
                "username" => "用户名已存在!",
                "email" => "邮箱已存在!",
                "phone" => "手机已存在!"
            ]);
            if (true !== $vaResult) {
                // 验证失败 输出错误信息
                return json(["code" => 2, "msg" => "{$vaResult}err:2"]);
            }
            $result = $sysAdminModel->addUserData($data);
            if ($result != false) {
                return json(["code" => 0, "msg" => "添加成功"]);
            } else {
                return json(["code" => 1, "msg" => "失败!err:1"]);
            }
        } else {
            $roleList = $sysRoleModel->select();
            $this->assign("roleList", $roleList);
            return $this->fetch();
        }
    }

    public function userDel(Request $request, SysAdminModel $sysAdminModel)
    {
        $admin_id = $request->param("admin_id");
        if (!empty($admin_id)) {
            $result = $sysAdminModel->delUserById($admin_id);
            if ($result != false) {
                return json(["code" => 0, "msg" => "删除成功"]);
            } else {
                return json(["code" => 1, "msg" => "失败!err:1"]);
            }
        }
    }

    public function userUpd(Request $request, SysAdminModel $sysAdminModel,SysRoleModel $sysRoleModel)
    {
        if ($request->isAjax()) {
            $data = $request->param();
            $admin_id = $data["admin_id"];
            $vaResult = $this->validate($data, [
                "username" => "unique:sys_admin",
                "email" => "unique:sys_admin",
                "phone" => "unique:sys_admin",
            ], [
                "username" => "用户名已存在!",
                "email" => "邮箱已存在!",
                "phone" => "手机已存在!"
            ]);
            if (true !== $vaResult) {
                // 验证失败 输出错误信息
                return json(["code" => 2, "msg" => "{$vaResult}err:2"]);
            }
            $result = $sysAdminModel->updUserData($data, $admin_id);
            if ($result != false) {
                return json(["code" => 0, "msg" => "修改成功"]);
            } else {
                return json(["code" => 1, "msg" => "失败!err:1"]);
            }
        } else {
            $admin_id = $request->param("admin_id");
            $user = $sysAdminModel->getUserById($admin_id);
            $roleList = $sysRoleModel->select();
            $this->assign([
                "user" => $user,
                "roleList" => $roleList
            ]);
            return $this->fetch();
        }
    }
}
